<template>
 <div class="ranking-container">
    <div class="ranking-title">小说排行榜</div>
    <div class="ranking-content">
      <div class="ranking-section">
        <div class="section-title">点击榜</div>
        <ul class="novel-list">
          <li v-for="(item, index) in novelListByClick" :key="item.title" class="novel-item" @click="goToDetail(item.title)">
            <div class="rank-number" :class="{'top-three': index < 3}">{{ index + 1 }}</div>
            <div v-if="index < 3" class="novel-cover">
              <img :src="item.coverUrl" alt="封面" />
            </div>
            <div class="novel-info">
              <div class="novel-title">{{ item.title }}</div>
              <div class="novel-meta">
                <span class="novel-author">{{ item.authorName }}</span>
                <span class="novel-category">{{ item.categoryName }}</span>
              </div>
              <div class="novel-count">点击量：{{ item.clicks }}</div>
            </div>
          </li>
        </ul>
      </div>
      
      <div class="ranking-section">
        <div class="section-title">月票榜</div>
        <ul class="novel-list">
          <li v-for="(item, index) in novelListByPass" :key="item.title" class="novel-item" @click="goToDetail(item.title)">
            <div class="rank-number" :class="{'top-three': index < 3}">{{ index + 1 }}</div>
            <div v-if="index < 3" class="novel-cover">
              <img :src="item.coverUrl" alt="封面" />
            </div>
            <div class="novel-info">
              <div class="novel-title">{{ item.title }}</div>
              <div class="novel-meta">
                <span class="novel-author">{{ item.authorName }}</span>
                <span class="novel-category">{{ item.categoryName }}</span>
              </div>
              <div class="novel-count">月票：{{ item.pass }}</div>
            </div>
          </li>
        </ul>
      </div>
      
      <div class="ranking-section">
        <div class="section-title">收藏榜</div>
        <ul class="novel-list">
          <li v-for="(item, index) in novelListByCollection" :key="item.title" class="novel-item" @click="goToDetail(item.title)">
            <div class="rank-number" :class="{'top-three': index < 3}">{{ index + 1 }}</div>
            <div v-if="index < 3" class="novel-cover">
              <img :src="item.coverUrl" alt="封面" />
            </div>
            <div class="novel-info">
              <div class="novel-title">{{ item.title }}</div>
              <div class="novel-meta">
                <span class="novel-author">{{ item.authorName }}</span>
                <span class="novel-category">{{ item.categoryName }}</span>
              </div>
              <div class="novel-count">收藏：{{ item.collection || 0 }}</div>
            </div>
          </li>
        </ul>
      </div>
      
      <div class="ranking-section">
        <div class="section-title">本月新书榜</div>
        <ul class="novel-list">
          <li v-for="(item, index) in novelListByMonth" :key="item.title" class="novel-item" @click="goToDetail(item.title)">
            <div class="rank-number" :class="{'top-three': index < 3}">{{ index + 1 }}</div>
            <div v-if="index < 3" class="novel-cover">
              <img :src="item.coverUrl" alt="封面" />
            </div>
            <div class="novel-info">
              <div class="novel-title">{{ item.title }}</div>
              <div class="novel-meta">
                <span class="novel-author">{{ item.authorName }}</span>
                <span class="novel-category">{{ item.categoryName }}</span>
              </div>
              <div class="novel-count">收藏：{{ item.collection || 0 }}</div>
            </div>
          </li>
        </ul>
      </div>
    </div>
    
    <!-- 猜你喜欢部分 -->
    <div class="recommend-section">
      <div class="recommend-title">猜你喜欢</div>
      <div class="recommend-content">
        <div v-for="(item, index) in recommendList" :key="item.title" class="recommend-item" @click="goToDetail(item.title)">
          <div class="recommend-cover">
            <img :src="item.coverUrl" alt="封面" />
          </div>
          <div class="recommend-info">
            <div class="recommend-title-text">{{ item.title }}</div>
            <div class="recommend-meta">
              <span class="recommend-author">{{ item.authorName }}</span>
              <span class="recommend-category">{{ item.categoryName }}</span>
            </div>
            <div class="recommend-collection">收藏：{{ item.collection || 0 }}</div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { listNovel, getNovel, delNovel, addNovel, updateNovel, listNovelByClick, listNovelByPass, listNovelByCollection, listNovelByMonth, ListByIntelligent } from "@/api/novel/novel";
import store from "@/store";
export default {
  name: "Novel",
  data() {
    return {
      userId:store.state.user.id,//将缓存的用户id赋值给userId
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 小说列表表格数据
      novelList: [],
      novelListByClick: [],
      novelListByCollection: [],
      novelListByMonth: [],
      novelListByPass: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        categoryName: null,
        status: null,
        wordCount: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        title: [
          { required: true, message: "小说标题不能为空", trigger: "blur" }
        ],
        authorId: [
          { required: true, message: "作者ID不能为空", trigger: "blur" }
        ],
        authorName: [
          { required: true, message: "作者名字不能为空", trigger: "blur" }
        ],
        categoryId: [
          { required: true, message: "分类ID不能为空", trigger: "blur" }
        ],
        categoryName: [
          { required: true, message: "分类名称不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "状态不能为空", trigger: "change" }
        ],
      },
      // 猜你喜欢
      recommendList: []
    };
  },
  created() {
    this.getList();
    this.listNovelByClick();
    this.listNovelByPass();
    this.listNovelByCollection();
    this.listNovelByMonth();
    this.getRecommendList();
  },
  methods: {
    /** 查询小说列表列表 */
    getList() {
      this.loading = true;
      listNovel(this.queryParams).then(response => {
        this.novelList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 根据点击量对小说排行
    listNovelByClick() {
      this.loading = true;
      listNovelByClick(this.queryParams).then(response => {
        this.novelListByClick = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 根据收藏量对小说排行
    listNovelByCollection() {
      this.loading = true;
      listNovelByCollection(this.queryParams).then(response => {
        this.novelListByCollection = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 根据月票对小说排行
    listNovelByMonth() {
      this.loading = true;
      listNovelByMonth(this.queryParams).then(response => {
        this.novelListByMonth = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 根据月票对小说排行
    listNovelByPass() {
      this.loading = true;
      listNovelByPass(this.queryParams).then(response => {
        this.novelListByPass = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 根据用户推荐小说
    ListByIntelligent() {
      if (!this.userId) {
        this.$message.error('请先登录');
        return;
      }
      this.loading = true;
      ListByIntelligent(this.userId).then(response => {
        this.novelListByIntelligent = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        title: null,
        authorId: null,
        authorName: null,
        categoryId: null,
        categoryName: null,
        status: null,
        description: null,
        coverUrl: null,
        wordCount: null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加小说列表";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getNovel(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改小说列表";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateNovel(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addNovel(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除小说列表编号为"' + ids + '"的数据项？').then(function() {
        return delNovel(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('novel/novel/export', {
        ...this.queryParams
      }, `novel_${new Date().getTime()}.xlsx`)
    },
    // 获取猜你喜欢
    getRecommendList() {
      const userId = store.state.user.id;
      if (!userId) return;
      
      ListByIntelligent(userId).then(response => {
        this.recommendList = response.rows || [];
      }).catch(error => {
        console.error('获取推荐列表失败:', error);
      });
    },
    goToDetail(title) {
      if (!title) {
        this.$message.error('小说标题不能为空');
        return;
      }
      this.$router.push({
        path: '/novel/detail',
        query: { 
          title: title.toString() // 确保title是字符串类型
        }
      });
    }
  }
};
</script>

<style scoped>
.ranking-container {
  padding: 20px;
  background: linear-gradient(to bottom, #f8f9fa, #ffffff);
  border-radius: 12px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
  min-height: 50vh;
  display: flex;
  flex-direction: column;
}

.ranking-title {
  font-size: 24px;
  color: #2c3e50;
  margin-bottom: 20px;
  padding-bottom: 15px;
  border-bottom: 2px solid #eef2f7;
  font-weight: 600;
  text-align: center;
  position: relative;
}

.ranking-title::after {
  content: '';
  position: absolute;
  bottom: -2px;
  left: 50%;
  transform: translateX(-50%);
  width: 60px;
  height: 2px;
  background: #f56c6c;
}

.ranking-content {
  display: flex;
  flex-direction: row;
  gap: 20px;
  flex: 1;
  overflow-x: auto;
  margin-bottom: 30px;
  height: 200px;
  padding: 5px;
}

.ranking-section {
  flex: 1;
  min-width: 220px;
  background: #ffffff;
  border-radius: 12px;
  padding: 12px;
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.05);
  display: flex;
  flex-direction: column;
  height: 100%;
  border-top: 3px solid #f56c6c;
  transition: transform 0.3s ease;
}

.ranking-section:hover {
  transform: translateY(-5px);
}

.section-title {
  font-size: 16px;
  color: #2c3e50;
  margin-bottom: 10px;
  font-weight: 600;
  padding-bottom: 8px;
  border-bottom: 1px solid #eef2f7;
  text-align: center;
  position: relative;
}

.section-title::after {
  content: '';
  position: absolute;
  bottom: -1px;
  left: 50%;
  transform: translateX(-50%);
  width: 40px;
  height: 1px;
  background: #f56c6c;
}

.novel-list {
  list-style: none;
  padding: 0;
  margin: 0;
  flex: 1;
  overflow-y: auto;
  max-height: 100%;
  scrollbar-width: thin;
  scrollbar-color: #f56c6c #f0f0f0;
}

.novel-list::-webkit-scrollbar {
  width: 6px;
}

.novel-list::-webkit-scrollbar-track {
  background: #f0f0f0;
  border-radius: 3px;
}

.novel-list::-webkit-scrollbar-thumb {
  background: #f56c6c;
  border-radius: 3px;
}

.novel-item {
  display: flex;
  align-items: center;
  margin-bottom: 8px;
  padding: 8px;
  border-radius: 8px;
  transition: all 0.3s ease;
  background-color: #ffffff;
  border-left: 3px solid transparent;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
  cursor: pointer;
}

.novel-item:hover {
  background-color: #fff5f5;
  transform: translateX(5px);
  box-shadow: 0 4px 12px rgba(245, 108, 108, 0.1);
  border-left: 3px solid #f56c6c;
}

.rank-number {
  width: 24px;
  height: 24px;
  line-height: 24px;
  text-align: center;
  background: linear-gradient(135deg, #f56c6c, #ff9a9e);
  border-radius: 50%;
  margin-right: 10px;
  font-size: 12px;
  font-weight: bold;
  flex-shrink: 0;
  color: #fff;
  box-shadow: 0 2px 6px rgba(245, 108, 108, 0.2);
}

.top-three {
  background: linear-gradient(135deg, #f56c6c, #ff9a9e);
}

.novel-cover {
  width: 50px;
  height: 70px;
  margin-right: 10px;
  border-radius: 6px;
  overflow: hidden;
  flex-shrink: 0;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.novel-cover img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.3s ease;
}

.novel-item:hover .novel-cover img {
  transform: scale(1.08);
}

.novel-info {
  flex: 1;
  min-width: 0;
}

.novel-title {
  font-size: 14px;
  font-weight: 600;
  color: #2c3e50;
  margin-bottom: 4px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.novel-meta {
  display: flex;
  align-items: center;
  margin-bottom: 4px;
  gap: 8px;
}

.novel-author {
  font-size: 12px;
  color: #606f7b;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.novel-category {
  font-size: 11px;
  color: #f56c6c;
  background-color: #fff5f5;
  padding: 2px 6px;
  border-radius: 10px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.novel-count {
  font-size: 12px;
  color: #f56c6c;
  font-weight: 600;
}

/* 猜你喜欢部分样式 */
.recommend-section {
  margin-top: 30px;
  background: #ffffff;
  border-radius: 12px;
  padding: 25px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
}

.recommend-title {
  font-size: 22px;
  color: #2c3e50;
  margin-bottom: 25px;
  padding-bottom: 15px;
  border-bottom: 2px solid #eef2f7;
  font-weight: 600;
  text-align: center;
  position: relative;
}

.recommend-title::after {
  content: '';
  position: absolute;
  bottom: -2px;
  left: 50%;
  transform: translateX(-50%);
  width: 60px;
  height: 2px;
  background: #f56c6c;
}

.recommend-content {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 25px;
}

.recommend-item {
  background: #ffffff;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.05);
  transition: all 0.3s ease;
  cursor: pointer;
}

.recommend-item:hover {
  transform: translateY(-8px);
  box-shadow: 0 8px 25px rgba(245, 108, 108, 0.15);
}

.recommend-cover {
  position: relative;
  height: 220px;
  overflow: hidden;
  background-color: #f8f9fa;
  display: flex;
  align-items: center;
  justify-content: center;
}

.recommend-cover img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  transition: transform 0.3s ease;
  background-color: #fff;
}

.recommend-item:hover .recommend-cover img {
  transform: scale(1.08);
}

.recommend-info {
  padding: 18px;
  background: linear-gradient(to bottom, #ffffff, #f8f9fa);
}

.recommend-title-text {
  font-size: 16px;
  font-weight: 600;
  color: #2c3e50;
  margin-bottom: 10px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.recommend-meta {
  display: flex;
  align-items: center;
  margin-bottom: 10px;
  gap: 10px;
}

.recommend-author {
  font-size: 14px;
  color: #606f7b;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.recommend-category {
  font-size: 12px;
  color: #f56c6c;
  background-color: #fff5f5;
  padding: 3px 8px;
  border-radius: 12px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.recommend-collection {
  font-size: 14px;
  color: #f56c6c;
  font-weight: 600;
}

.novel-item, .recommend-item {
  cursor: pointer;
  transition: all 0.3s ease;
}

.novel-item:hover, .recommend-item:hover {
  transform: translateY(-5px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}
</style>
